package com.longrise.android.byjk.plugins.dealsituation.course.score;

import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BasePresenter;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.TimeHelper;


/**
 * Created by godliness on 2017/8/15.
 */

public class ScorePresenter extends BasePresenter<ScoreView> {

    private static final String TAG = "ScorePresenter";

    private final int mRequestPF = 0;
    private final int mCommentPF = 1;
    private int mCurrentType = mRequestPF;

    @Override
    protected void init() {

    }

    /**
     * 请求评分信息
     */
    public void toRequestPF(String courseId) {
        EntityBean bean = new EntityBean();
        bean.set("scoreuser", UserInfor.getInstance().getUserid());
        bean.set("scoreobj", courseId);
        EntityBean serBean = new EntityBean();
        serBean.set("bean", bean);
        mCurrentType = mRequestPF;
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_course_sCourseScoreBean", serBean, mCallback);
    }

    /**
     * 评分
     */
    public void toCommentRequest(String courseId, final int bar1, int bar2) {
        EntityBean bean = new EntityBean();
        bean.set("scoreuser", UserInfor.getInstance().getUserid());
        bean.set("scoreobj", courseId);
        bean.set("score0", bar1);
        bean.set("score1", bar2);
        bean.set("scoreopcount", 2);
        bean.set("fullmark", 5);
        EntityBean serBean = new EntityBean();
        serBean.set("bean", bean);
        mCurrentType = mCommentPF;
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_course_iCourseScore", serBean, mCallback);
    }

    private LoadDataManager.OnRequestCallListener mCallback = new LoadDataManager.OnRequestCallListener() {
        @Override
        public void onSuccess(String key, String service, Object result) {
            if (mView.actFinished()) {
                return;
            }
            if (result instanceof EntityBean) {
                EntityBean resultBean = (EntityBean) result;
                int resultstate = resultBean.getInt("resultstate");
                if (resultstate != 1) {
                    mView.showToast(resultBean.getString("resultdesc"));
                    return;
                }
                EntityBean resultStr = resultBean.getBean("result");
                if (mCurrentType == mRequestPF) {
                    try {
                        parse(resultStr);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return;
                }
                //评分
                mView.showCommentSuccessDialog();
            }
        }

        @Override
        public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
            DZZWTools.showToast(mContext, mContext.getResources().getString(R.string.networkerr), Toast.LENGTH_SHORT);
        }

        @Override
        public void onFinished(String key, String service) {
            DZZWTools.dismissDialog(null);
        }
    };

    /**
     * 解析评分信息
     */
    private void parse(EntityBean resultBean) throws Exception {
        if (resultBean == null) {
            return;
        }
        String name = resultBean.getString("name"); //課程名稱
        String avgscore = resultBean.getString("avgscore"); //課程平均分
        String realpic = resultBean.getString("realpic");
        String grouptime = resultBean.getString("grouptime");

        String resulttime = TimeHelper.formateTime(grouptime);
        String time = "时长: " + resulttime;

        //讲师
        String teacher = resultBean.getString("teachername");
        if (TextUtils.isEmpty(teacher)) {
            teacher = "";
        }

        mView.setCourseInfor(realpic, "讲师: " + teacher, time, name, avgscore);
        Float avgValue = Float.parseFloat(avgscore);

        EntityBean myscore = (EntityBean) resultBean.get("myscore");
        if (myscore != null) {
            boolean ishavemyscore = myscore.getBoolean("ishavemyscore");
            if (!ishavemyscore) {
                mView.setCommentBtnState(View.VISIBLE);
            }
            double score0 = 0;
            double score1 = 0;
            if (!myscore.isNull("score0")) {
                score0 = myscore.getDouble("score0");
            }
            if (!myscore.isNull("score1")) {
                score1 = myscore.getDouble("score1");
            }
            mView.setRatingValue(ishavemyscore, avgValue, (int) score0, (int) score1);
        }
    }
}
